import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;

@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 模拟从数据库或其他数据源获取数据
        String[] data = {"数据1", "数据2", "数据3"};

        // 将数据转换为 JSON 格式
        String jsonData = JSON.toJSONString(data);
        //System.out.print(jsonData);

        // 设置响应内容类型为 JSON
        response.setContentType("application/json");
        response.setCharacterEncoding("UTF-8");

        // 将 JSON 数据写入响应
        PrintWriter out = response.getWriter();
        out.write(jsonData);
        out.flush();
    }
}
